#ifndef DmxClient_H
#define DmxClient_H

#include <QDialog>
#include <QTcpSocket>

#include "DmxServer.h" // for DMX_FRAME_SIZE
#include "DmxValueList.h"

class QTcpSocket;

class DmxClient : public QObject
{
	Q_OBJECT

public:
	DmxClient(bool verbose=false, QObject *parent = 0);
	~DmxClient();
	
	bool start(const QString& host="localhost", int port=6159);
	void exit();
	
	bool isConnected() { return m_connected; }
	
	bool isSwitchToggled() { return m_isSwitchToggled; }
	bool isSwitchMaster() { return m_isSwitchMaster; }
	DmxValueList channelStates() { return m_channelStates; }
	DmxValueList switchOnProfile() { return m_switchOnProfile; }
	
public slots:
	// This should be called by the GUI module
	void setSwitchMaster(bool);
	
// 	// Transmit a single DMX value.
// 	// Really, just calls setDmxChannels() with the appros array index updated
 	void setDmxChannel(int chan, int val); // chan 0-512, val 0-255
	
	// Set all channels in the current DMX universe in one go
	// TODO: How do we change the DMX universe?
	// NOTE: If isSwitchMaster() is TRUE, then this has NO EFFECT
	void setDmxChannels(DmxValueList);
	
	// Store a new "switch on" profile (default is all channels=255)
	void setSwitchOnProfile(DmxValueList);
	
signals:
	// From the dmx server
	void switchToggled(bool);
	
	// Network-related signals
	void socketDisconnected();
	void socketError(QAbstractSocket::SocketError);
	void error(QString);	
	void socketConnected();
	void connected();
	void connectionStatusChanged(bool isConnected);

private slots:
	void dataReady();
	void processBlock();
	void lostConnection();
	void lostConnection(QAbstractSocket::SocketError);
	void reconnect();
	void connectionReady();

	void emitError(QAbstractSocket::SocketError socketError);

private:
	DmxValueList m_channelStates;
	DmxValueList m_switchOnProfile;
	bool m_isSwitchToggled;
	bool m_isSwitchMaster;
	
	QTcpSocket *m_socket;
	QByteArray m_dataBlock;
	bool m_connected;
	QString m_host;
	int m_port;
	bool m_verbose;
	bool m_autoReconnect;
	char m_dataFrame[DMX_SERVER_FRAME_SIZE];

};

#endif
